﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using transportsystem.DTO;
using UTIL;

namespace transportsystem.DAO
{
    public class XeDAO
    {

        public static DataTable getXeList()
        {
            SqlDataAccessHelper.ConnectSQLServer();
            string procName = "sp_TraCuuXe '', '', '', '', '', ''";
            DataSet ds = SqlDataAccessHelper.LoadData(procName);
            if (ds != null)
                return ds.Tables[0];
            return null;
        }

        public static DataTable getXeListByBienSo(string bienSo, int action)
        {
            SqlDataAccessHelper.ConnectSQLServer();
            string procName;
            if (action == 0)
                procName = "sp_TraCuuXeTheoBienSo";
            else
                procName = "sp_TraCuuXeTheoBienSoGQ";
            SqlParameter[] sp = new SqlParameter[1];
            sp[0] = new SqlParameter("@bienso", bienSo);
            DataSet ds = SqlDataAccessHelper.LoadDataWithParameter(procName, sp);
            if (ds != null)
                return ds.Tables[0];
            return null;
        }

        public static DataTable getXeByMaDatVe(int maDatVe)
        {
            SqlDataAccessHelper.ConnectSQLServer();
            string procName = "sp_TraCuuXeTheoMaDatVe";
            SqlParameter[] sp = new SqlParameter[1];
            sp[0] = new SqlParameter("@madatve", maDatVe);
            DataSet ds = SqlDataAccessHelper.LoadDataWithParameter(procName, sp);
            if (ds != null)
                return ds.Tables[0];
            return null;
        }

        public static int getSoLuongGheByMaXe(int maXe)
        {
            SqlDataAccessHelper.ConnectSQLServer();
            string procName = "sp_TraCuuSoLuongGheTheoMaXe";
            SqlParameter[] sp = new SqlParameter[1];
            sp[0] = new SqlParameter("@maxe", maXe);
            DataSet ds = SqlDataAccessHelper.LoadDataWithParameter(procName, sp);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
                return int.Parse(ds.Tables[0].Rows[0]["soluongghe"].ToString());
            return 0;
        }

        public static bool updateXe(Xe x, int action, int tc, int gq)
        {
            SqlCommand cmd;
            if (tc == 0)
            {
                if (gq == 0)
                    cmd = SqlDataAccessHelper.CreateCommand("sp_ThemXeDirty");
                else
                    cmd = SqlDataAccessHelper.CreateCommand("sp_ThemXeDirtyGQ");
            }
            else
            {
                if (gq == 0)
                    cmd = SqlDataAccessHelper.CreateCommand("sp_ThemXeLost");
                else
                    cmd = SqlDataAccessHelper.CreateCommand("sp_ThemXeLostGQ");
            }

            cmd.Parameters.Add("@maxe", SqlDbType.Int).Value = x.Maxe;
            cmd.Parameters.Add("@bangsoxe", SqlDbType.NVarChar).Value = x.Bangsoxe;
            cmd.Parameters.Add("@action", SqlDbType.Int).Value = action;
            cmd.Parameters.Add("@soluongghethem", SqlDbType.Int).Value = x.Soluongghe;
            cmd.Parameters.Add("@soluongghe", SqlDbType.Int).Value = x.Soluongghe;
            cmd.Parameters.Add("@trangthai", SqlDbType.Int).Value = x.Trangthai;
            cmd.Parameters.Add("@maloaixe", SqlDbType.Int).Value = x.Maloaixe;
            cmd.Parameters.Add("@trongluongtaihang", SqlDbType.Int).Value = x.Trongluonghangtai;

            return SqlDataAccessHelper.ExecCmd(cmd);
        }

        public static bool deleteXe(Xe x)
        {
            SqlCommand cmd = SqlDataAccessHelper.CreateCommand("sp_XoaXe");
            cmd.Parameters.Add("@maxe", SqlDbType.Int).Value = x.Maxe;
            return SqlDataAccessHelper.ExecCmd(cmd);
        }
        public static bool updateGhe(params SqlParameter[] Params)
        {
            SqlDataAccessHelper.ConnectSQLServer();
            string procName = "sp_CapNhatSoGhe";
            DataSet ds = SqlDataAccessHelper.LoadDataWithParameter(procName, Params);
            if (ds != null)
                return true;
            return false;
        }
    }
}
